
CREATE TABLE blog_article_y2020 (CHECK ( post_at >= DATE '2020-01-01' AND post_at < DATE '2021-01-01' )) INHERITS (blog_article);
CREATE INDEX idx_blog_article_y2020_post_at ON blog_article_y2020 (post_at);

CREATE TABLE blog_article_y2019 (CHECK ( post_at >= DATE '2019-01-01' AND post_at < DATE '2020-01-01' )) INHERITS (blog_article);
CREATE INDEX idx_blog_article_y2019_post_at ON blog_article_y2019 (post_at);

CREATE TABLE blog_article_y2018 (CHECK ( post_at >= DATE '2018-01-01' AND post_at < DATE '2019-01-01' )) INHERITS (blog_article);
CREATE INDEX idx_blog_article_y2018_post_at ON blog_article_y2018 (post_at);

CREATE TABLE blog_article_y2017 (CHECK ( post_at >= DATE '2017-01-01' AND post_at < DATE '2018-01-01' )) INHERITS (blog_article);
CREATE INDEX idx_blog_article_y2017_post_at ON blog_article_y2017 (post_at);

CREATE TABLE blog_article_y2016 (CHECK ( post_at >= DATE '2016-01-01' AND post_at < DATE '2017-01-01' )) INHERITS (blog_article);
CREATE INDEX idx_blog_article_y2016_post_at ON blog_article_y2016 (post_at);

CREATE TABLE blog_article_y2015 (CHECK ( post_at >= DATE '2015-01-01' AND post_at < DATE '2016-01-01' )) INHERITS (blog_article);
CREATE INDEX idx_blog_article_y2015_post_at ON blog_article_y2015 (post_at);

CREATE TABLE blog_article_y2014 (CHECK ( post_at >= DATE '2014-01-01' AND post_at < DATE '2015-01-01' )) INHERITS (blog_article);
CREATE INDEX idx_blog_article_y2014_post_at ON blog_article_y2014 (post_at);

CREATE TABLE blog_article_y2013 (CHECK ( post_at >= DATE '2013-01-01' AND post_at < DATE '2014-01-01' )) INHERITS (blog_article);
CREATE INDEX idx_blog_article_y2013_post_at ON blog_article_y2013 (post_at);

CREATE TABLE blog_article_y2012 (CHECK ( post_at >= DATE '2012-01-01' AND post_at < DATE '2013-01-01' )) INHERITS (blog_article);
CREATE INDEX idx_blog_article_y2012_post_at ON blog_article_y2012 (post_at);

CREATE TABLE blog_article_y2011 (CHECK ( post_at >= DATE '2011-01-01' AND post_at < DATE '2012-01-01' )) INHERITS (blog_article);
CREATE INDEX idx_blog_article_y2011_post_at ON blog_article_y2011 (post_at);

CREATE TABLE blog_article_y2010 (CHECK ( post_at >= DATE '2010-01-01' AND post_at < DATE '2011-01-01' )) INHERITS (blog_article);
CREATE INDEX idx_blog_article_y2010_post_at ON blog_article_y2010 (post_at);

CREATE OR REPLACE FUNCTION blog_article_insert_trigger()
RETURNS TRIGGER AS $$
BEGIN
    
        
    IF ( NEW.post_at >= DATE '2020-01-01' AND
         NEW.post_at < DATE '2021-01-01' ) THEN
        INSERT INTO blog_article_y2020 VALUES (NEW.*);        
        
    
        
        ELSIF ( NEW.post_at >= DATE '2019-01-01' AND
            NEW.post_at < DATE '2020-01-01' ) THEN
        INSERT INTO blog_article_y2019 VALUES (NEW.*); 
        
    
        
        ELSIF ( NEW.post_at >= DATE '2018-01-01' AND
            NEW.post_at < DATE '2019-01-01' ) THEN
        INSERT INTO blog_article_y2018 VALUES (NEW.*); 
        
    
        
        ELSIF ( NEW.post_at >= DATE '2017-01-01' AND
            NEW.post_at < DATE '2018-01-01' ) THEN
        INSERT INTO blog_article_y2017 VALUES (NEW.*); 
        
    
        
        ELSIF ( NEW.post_at >= DATE '2016-01-01' AND
            NEW.post_at < DATE '2017-01-01' ) THEN
        INSERT INTO blog_article_y2016 VALUES (NEW.*); 
        
    
        
        ELSIF ( NEW.post_at >= DATE '2015-01-01' AND
            NEW.post_at < DATE '2016-01-01' ) THEN
        INSERT INTO blog_article_y2015 VALUES (NEW.*); 
        
    
        
        ELSIF ( NEW.post_at >= DATE '2014-01-01' AND
            NEW.post_at < DATE '2015-01-01' ) THEN
        INSERT INTO blog_article_y2014 VALUES (NEW.*); 
        
    
        
        ELSIF ( NEW.post_at >= DATE '2013-01-01' AND
            NEW.post_at < DATE '2014-01-01' ) THEN
        INSERT INTO blog_article_y2013 VALUES (NEW.*); 
        
    
        
        ELSIF ( NEW.post_at >= DATE '2012-01-01' AND
            NEW.post_at < DATE '2013-01-01' ) THEN
        INSERT INTO blog_article_y2012 VALUES (NEW.*); 
        
    
        
        ELSIF ( NEW.post_at >= DATE '2011-01-01' AND
            NEW.post_at < DATE '2012-01-01' ) THEN
        INSERT INTO blog_article_y2011 VALUES (NEW.*); 
        
    
        
        ELSIF ( NEW.post_at >= DATE '2010-01-01' AND
            NEW.post_at < DATE '2011-01-01' ) THEN
        INSERT INTO blog_article_y2010 VALUES (NEW.*); 
        
        
    ELSE
        RAISE EXCEPTION 'Date out of range.  Fix the blog_article_insert_trigger() function!';
    END IF;
    RETURN NULL;
END;
$$
LANGUAGE plpgsql;
/*
CREATE OR REPLACE FUNCTION blog_article_delete_trigger()
RETURNS TRIGGER AS $$
BEGIN

 
        
    IF ( OLD.post_at >= DATE '2020-01-01' AND
         OLD.post_at < DATE '2021-01-01' ) THEN
        DELETE FROM blog_article_y2020 WHERE post_at=OLD.post_at;   
        
    
        
       ELSIF ( OLD.post_at >= DATE '2019-01-01' AND OLD.post_at < DATE '2016-04-01' ) THEN
        DELETE FROM blog_article_y2019 WHERE post_at=OLD.post_at;
        
    
        
       ELSIF ( OLD.post_at >= DATE '2018-01-01' AND OLD.post_at < DATE '2016-04-01' ) THEN
        DELETE FROM blog_article_y2018 WHERE post_at=OLD.post_at;
        
    
        
       ELSIF ( OLD.post_at >= DATE '2017-01-01' AND OLD.post_at < DATE '2016-04-01' ) THEN
        DELETE FROM blog_article_y2017 WHERE post_at=OLD.post_at;
        
    
        
       ELSIF ( OLD.post_at >= DATE '2016-01-01' AND OLD.post_at < DATE '2016-04-01' ) THEN
        DELETE FROM blog_article_y2016 WHERE post_at=OLD.post_at;
        
    
        
       ELSIF ( OLD.post_at >= DATE '2015-01-01' AND OLD.post_at < DATE '2016-04-01' ) THEN
        DELETE FROM blog_article_y2015 WHERE post_at=OLD.post_at;
        
    
        
       ELSIF ( OLD.post_at >= DATE '2014-01-01' AND OLD.post_at < DATE '2016-04-01' ) THEN
        DELETE FROM blog_article_y2014 WHERE post_at=OLD.post_at;
        
    
        
       ELSIF ( OLD.post_at >= DATE '2013-01-01' AND OLD.post_at < DATE '2016-04-01' ) THEN
        DELETE FROM blog_article_y2013 WHERE post_at=OLD.post_at;
        
    
        
       ELSIF ( OLD.post_at >= DATE '2012-01-01' AND OLD.post_at < DATE '2016-04-01' ) THEN
        DELETE FROM blog_article_y2012 WHERE post_at=OLD.post_at;
        
    
        
       ELSIF ( OLD.post_at >= DATE '2011-01-01' AND OLD.post_at < DATE '2016-04-01' ) THEN
        DELETE FROM blog_article_y2011 WHERE post_at=OLD.post_at;
        
    
        
       ELSIF ( OLD.post_at >= DATE '2010-01-01' AND OLD.post_at < DATE '2016-04-01' ) THEN
        DELETE FROM blog_article_y2010 WHERE post_at=OLD.post_at;
        
        
    
ELSE
        RAISE EXCEPTION 'Date out of range.  Fix the blog_article_insert_trigger() function!';
    END IF;
    RETURN NULL;
END;
$$ LANGUAGE plpgsql;
*/
CREATE TRIGGER insert_blog_article_trigger
    BEFORE INSERT ON blog_article
    FOR EACH ROW EXECUTE PROCEDURE blog_article_insert_trigger();
/*
CREATE TRIGGER delete_blog_article_trigger
    BEFORE DELETE ON blog_article
    FOR EACH ROW EXECUTE PROCEDURE blog_article_delete_trigger();
*/